package com.ruoyi.system.mapper;

import com.ruoyi.system.domain.Dto.SpendDetailDto;
import com.ruoyi.system.domain.SpendDetail;
import com.ruoyi.system.domain.SysSpend;
import com.ruoyi.system.domain.SysTotalBill;
import com.ruoyi.system.domain.vo.SpendDetailVo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;

/**
 * @Author chenyouliang
 * @create 2024/11/14 10:06
 */
@Mapper
public interface SpendMapper {

    /**
     * 获取花销列表
     *
     * @param sysSpend
     * @return
     */
    List<SysSpend> selectSpendList(SysSpend sysSpend);

    /**
     * 添加花销记录
     *
     * @param spend
     * @return
     */
    int insertSpend(SysSpend spend);

    /**
     * 添加花销详情
     *
     * @param spendDetails
     */
    void insertSpendDetail(List<SpendDetail> spendDetails);

    /**
     * 删除花销列表
     *
     * @param ids
     */
    void deleteByIds(@Param("spendIds") Long[] spendIds);

    /**
     * 删除花销详情
     *
     * @param ids
     */
    void deleteDetailByIds(@Param("detailIds") List<Long> detailIds);

    /**
     * 删除单个花销详情
     *
     * @param id
     * @return
     */
    @Delete("delete from sys_spend_detail where id = #{id}")
    int deleteDetailById(Integer id);

    /**
     * 修改单个花销详情
     *
     * @param spendDetail
     * @return
     */
    int editDetail(SpendDetail spendDetail);

    /**
     * 获取花销
     *
     * @param id
     * @return
     */
    SysSpend getSpend(Integer id);

    /**
     * 获取花销详情
     *
     * @param id
     * @return
     */
    @Select("select id, name, price, spend_id from sys_spend_detail where spend_id = #{id}")
    List<SpendDetail> getSpendDetail(Integer id);

    /**
     * 获取单个花销详情
     *
     * @param id
     * @return
     */
    @Select("select id, name, price, spend_id from sys_spend_detail where id = #{id}")
    SpendDetail getDetailById(Integer id);

    /**
     * 修改信息
     *
     * @param spend
     */
    void updateSpend(SysSpend spend);

    /**
     * 获取账单详情列表
     *
     * @param spendDetailDto
     * @return
     */
    List<SpendDetail> selectDetailList(SpendDetailDto spendDetailDto);

    /**
     * 新增账单详情列表
     *
     * @param spendDetail
     * @return
     */
    int addDetailList(SpendDetail spendDetail);

    @Select("select id, spending_time, spending_price, create_by, create_time, update_by, update_time, remark, user_id, bill_id, actual_amount from sys_spend where spending_time = #{spendingTime} and user_id=#{userId}")
    List<SysSpend> getSpendByTime(@Param("spendingTime") LocalDate spendingTime, @Param("userId") Long userId);

    /**
     * 获取总账单列表
     *
     * @param sysTotalBill
     * @return
     */
    List<SysTotalBill> totalBillList(SysTotalBill sysTotalBill);

    /**
     * 获取总账单列表详情
     *
     * @param sysTotalBill
     * @return
     */
    @Select("select id, spend_total_price, wages, deposit, deposit_time, create_by, create_time, update_by, update_time, remark, user_id from sys_total_bill where id = #{id}")
    SysTotalBill totalBillDetail(SysTotalBill sysTotalBill);

    /**
     * 新增总账单
     *
     * @param sysTotalBill
     * @return
     */
    void totalBillAdd(SysTotalBill sysTotalBill);

    void totalBillUpdate(SysTotalBill sysTotalBill);

    @Select("SELECT SUM(spend_total_price) FROM sys_total_bill WHERE deposit_time BETWEEN #{startTime} AND #{endTime} and user_id = #{userId}")
    Double getTotalPrice(@Param("startTime") String startTime, @Param("endTime") String endTime, @Param("userId") Long userId);

    @Select("SELECT SUM(wages) FROM sys_total_bill WHERE deposit_time BETWEEN #{startTime} AND #{endTime} and user_id = #{userId}")
    Double getTotalWages(@Param("startTime") String startTime, @Param("endTime") String endTime, @Param("userId") Long userId);

    @Select("SELECT SUM(deposit) FROM sys_total_bill WHERE deposit_time BETWEEN #{startTime} AND #{endTime} and user_id = #{userId}")
    Double getTotalDeposit(@Param("startTime") String startTime, @Param("endTime") String endTime, @Param("userId") Long userId);

    @Select("SELECT MAX(actual_amount) FROM sys_spend WHERE spending_time BETWEEN #{startTimeDay} AND #{endTimeDay} and user_id = #{userId}")
    Double getTopPrice(@Param("startTimeDay") String startTimeDay, @Param("endTimeDay") String endTimeDay, @Param("userId") Long userId);

    @Select("select * from sys_total_bill where deposit_time = #{depositTime} and user_id=#{userId}")
    List<SysTotalBill> getDepositByTime(@Param("depositTime") String depositTime, @Param("userId") Long userId);

    @Select("select * from sys_total_bill where deposit_time BETWEEN #{startTime} and #{endTime} and user_id = #{userId} " +
            "order by spend_total_price desc limit 8")
    List<SysTotalBill> getTop(@Param("startTime") String startTime, @Param("endTime") String endTime, @Param("userId") Long userId);

    @Select("SELECT SUM(actual_amount) FROM sys_spend WHERE spending_time BETWEEN #{beginTime} AND #{endTime} and user_id = #{userId}")
    String getTotalActual(@Param("beginTime") String beginTime, @Param("endTime") String endTime, @Param("userId") Long userId);

    List<SpendDetailVo> getSpendDetailData(@Param("name") String name, @Param("userId") Long userId);
}
